import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split

import optuna
# 假设我们有一个数据集 data.csv，包含 x1, x2, x3 和 yi 列
data = pd.read_csv('data2.csv',encoding='gbk')

# 提取特征和目标变量
X = data[['x1', 'x2', 'x3']]
y1 = data['y']
y2=y1[1:1+48]
y = np.arange(16)
y = pd.Series(y)
for i in range(16):
    y[i]=(y2[i*3+1]+y2[i*3+2]+y2[i*3+3])/3
#y = y[]
#创建多项式特征
#X['x1_x2'] = X['x1'] * X['x2']
#X['x1_x3'] = X['x1'] * X['x3']
#X['x2_x3'] = X['x1'] * X['x2']
#X['x1_squared'] = X['x1'] ** 2
#X['x1_cube'] = X['x1'] ** 3
# X['x2_squared'] = X['x2'] ** 2
X['x3_squared'] = X['x3'] ** 2
X['x3_cube'] = X['x3'] ** 3

X=X[0:16]
# 标准化特征数据
# scaler = StandardScaler()
# X = scaler.fit_transform(X)

# 转换为 DataFrame
#X = pd.DataFrame(X, columns=X.columns)
#划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 创建线性回归模型
model = LinearRegression()

# 训练模型
model.fit(X, y)

def objective(trial):
    # 2. 使用trial对象建议超参数取值
    x1 = trial.suggest_categorical('x1', [15, 20, 25, 30])
    x2 = trial.suggest_categorical('x2', [100, 110, 120, 130])
    x3 = trial.suggest_categorical('x3', [0, 10, 20, 30])
    #data = loadData('mackey_glass_t17.npy')
    #max = model.coef_[0]*x1+model.coef_[1]*x2+model.coef_[2]*x3+model.coef_[3]*x1*x2+model.coef_[4]*x1*x3+model.coef_[5]*x2*x3+model.coef_[6]*x1*x1+model.coef_[7]*x1*x1*x1+model.coef_[8]*x2*x2+model.coef_[9]*x3*x3+model.intercept_;
    #max = model.coef_[0]*x1+model.coef_[1]*x2+model.coef_[2]*x3+model.coef_[3]*x1*x3+model.coef_[4]*x1*x1+model.coef_[5]*x3*x3+model.coef_[6]*x3*x3*x3+model.intercept_;
    #max = model.coef_[0]*x1+model.coef_[1]*x2+model.coef_[2]*x3+model.coef_[3]*x1*x1+model.coef_[4]*x3*x3+model.coef_[5]*x3*x3*x3+model.intercept_;
    #max = model.coef_[0]*x1+model.coef_[1]*x2+model.coef_[2]*x3+model.intercept_;
    #max = model.coef_[0]*x1+model.coef_[1]*x2+model.coef_[2]*x3+model.coef_[3]*x3*x3+model.intercept_;



    #max = model.coef_[0]*x1+model.coef_[1]*x2+model.coef_[2]*x3+model.coef_[3]*x1*x1+model.coef_[4]*x1*x1*x1+model.intercept_;
    #max = model.coef_[0]*x1+model.coef_[1]*x2+model.coef_[2]*x3+model.coef_[3]*x1*x2+model.coef_[4]*x1*x3+model.coef_[5]*x2*x3+model.coef_[6]*x1*x1+model.coef_[7]*x1*x1*x1+model.coef_[8]*x2*x2+model.coef_[9]*x3*x3+model.intercept_;

    #max = model.coef_[0]*x1+model.coef_[1]*x2+model.coef_[2]*x3+model.coef_[3]*x1*x3+model.coef_[4]*x1*x1+model.coef_[5]*x3*x3+model.coef_[6]*x3*x3*x3+model.intercept_;
    max = model.coef_[0]*x1+model.coef_[1]*x2+model.coef_[2]*x3+model.coef_[3]*x3*x3+model.coef_[4]*x3*x3*x3+model.intercept_;

    return max
# 创建Optuna study
study = optuna.create_study(direction='maximize')

 # 运行Optuna搜索
study.optimize(objective, n_trials=100)

# 打印最佳超参数和得分
print('Best hyperparameters: ', study.best_params)

print('Best score: ', study.best_value)




# 使用测试集进行预测
y_pred = model.predict(X_test)

# 计算均方误差
mse = mean_squared_error(y_test, y_pred)
print(f"均方误差: {mse}")

# 输出模型的系数和截距
print("模型系数:", model.coef_)
print("模型截距:", model.intercept_)
